package com.juzipi.demo.filter;

import com.juzipi.demo.utils.jwt.JwtUtil;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 自定义的登录鉴权过滤器
 */

public class JwtLoginFilter extends UsernamePasswordAuthenticationFilter {

    @Override
    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        //获取身份验证管理器?
        Authentication authenticate = getAuthenticationManager().authenticate(new UsernamePasswordAuthenticationToken(username, password));
        //已认证？
        if (authenticate.isAuthenticated()){
            User user = (User) authenticate.getPrincipal();
            String token = JwtUtil.getToken();
        }

    }
}
